package web_k;

import pojo_k.MainUser;
import service_k.MainUserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/mainUserRegisterServlet")
public class MainUserRegisterServlet extends HttpServlet {
    private MainUserService service = new MainUserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mainuserName = request.getParameter("mainuserName");
        String mainuserPasssword = request.getParameter("mainuserPassword");
        String mainuserTel = request.getParameter("mainuserTel");

        MainUser mainUser = new MainUser();
        mainUser.setMainuserName(mainuserName);
        mainUser.setMainuserPassword(mainuserPasssword);
        mainUser.setMainuserTel(mainuserTel);



        MainUser mainUser1 = service.selectByName(mainuserName);

        if (mainUser1 == null){
            //该用户名可用
            service.mainUserAdd(mainUser);
            response.sendRedirect("MainUserLogin.jsp");
        } else {
            //用户名已存在，提示用户重新输入
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("用户名已存在！");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
